<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目编号" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerId" style="width: 264.4px">
<!--        <el-input-->
<!--          v-model="queryParams.customerTitle"-->
<!--          placeholder="请输入客户名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
        <el-select
            v-model="queryParams.customerId" filterable remote reserve-keyword placeholder="请输入简称"
            :remote-method="remoteMethodOfCustomer" @change="handleChangeOfCustomer" :loading="loading" >
          <el-option
              v-for="item in optionsOfCustomer" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="ownerUserId" style="width: 264.4px">
        <el-select
            v-model="queryParams.ownerUserId" filterable remote reserve-keyword placeholder="请输入负责人"
            :remote-method="remoteMethodOfUser" @change="handleChangeOfUser" :loading="loading" >
          <el-option
              v-for="item in optionsOfUser" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.ownerUserName"-->
<!--          placeholder="请输入项目负责人名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate" style="width: 264.4px">
        <el-date-picker clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate" style="width: 264.4px">
        <el-date-picker clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目状态" prop="projectStatus" style="width: 264.4px">
        <el-select v-model="queryParams.projectStatus" placeholder="请选择项目状态" clearable >
          <el-option
            v-for="dict in scm_project_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="采购类型" prop="purchaseCategory">-->
<!--        <el-select v-model="queryParams.purchaseCategory" placeholder="请选择采购类型" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in scm_purchase_category"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="部品类型" prop="demandType">-->
<!--        <el-select v-model="queryParams.demandType" placeholder="请选择部品类型" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in scm_demand_type"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['scm:projects:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scm:projects:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scm:projects:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['scm:projects:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="projectId" />-->
      <el-table-column label="项目编号" align="left" prop="projectCode" width="140">
        <template #default="scope">
          <router-link :to="'/scm/scmProjectProducts?projectId=' + scope.row.projectId" class="link-type">
            <span>{{ scope.row.projectCode }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="left" prop="projectName" v-if="columns[0].visible" >
        <template #default="scope">
          <router-link :to="'/scm/scmProjectProducts?projectId=' + scope.row.projectId" class="link-type">
            <span>{{ scope.row.projectName }}</span>
          </router-link>
        </template>
      </el-table-column>
<!--      <el-table-column label="客户ID" align="center" prop="customerId" />-->
      <el-table-column label="客户" align="left" prop="customerTitle" width="80"/>
<!--      <el-table-column label="客户联系人ID" align="center" prop="customerContactId" />-->
      <el-table-column label="客户联系人" align="left" prop="customerContactName" width="90" />
<!--      <el-table-column label="项目负责人ID" align="center" prop="ownerUserId" />-->
      <el-table-column label="项目负责人" align="left" prop="ownerUserName" width="90" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="100" v-if="columns[1].visible">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="100" v-if="columns[2].visible" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="projectStatus" width="95" v-if="columns[3].visible">
        <template #default="scope">
          <dict-tag :options="scm_project_status" :value="scope.row.projectStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="采购类型" align="center" prop="purchaseCategory" width="95" v-if="columns[4].visible">
        <template #default="scope">
          <dict-tag :options="scm_purchase_category" :value="scope.row.purchaseCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="部品类型" align="center" prop="demandType" width="80" v-if="columns[5].visible">
        <template #default="scope">
          <dict-tag :options="scm_demand_type" :value="scope.row.demandType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="预算金额" align="left" prop="budget" />-->
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[6].visible" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:projects:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:projects:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目管理对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="projectsRef" :model="form" :rules="rules" label-width="92px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker clearable
                              v-model="form.startDate"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择开始日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目编号" prop="projectCode">
              <el-input v-model="form.projectCode" placeholder="请输入项目编号" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="客户全称" prop="customerId">
              <!--              <el-input v-model="form.customerTitle" placeholder="请输入客户名称" />-->
              <el-select
                  v-model="form.customerId" filterable remote reserve-keyword placeholder="请输入简称"
                  :remote-method="remoteMethodOfCustomer" @change="handleChangeOfCustomer" :loading="loading" >
                <el-option
                    v-for="item in optionsOfCustomer" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户简称" prop="customerTitle">
              <el-input v-model="form.customerTitle" placeholder="请输入客户简称" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
<!--          <el-col :span="12">-->
<!--            <el-form-item label="客户联系人" prop="customerContactName">-->
<!--              <el-input v-model="form.customerContactName" placeholder="请输入客户联系人" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="客户联系人" prop="customerContactId">
              <el-select
                  v-model="form.customerContactId" filterable remote reserve-keyword placeholder="请输入客户联系人"
                  :remote-method="remoteMethodOfCustomerContacts" @change="handleChangeOfCustomerContacts" :loading="loading" >
                <el-option label=" - " :value="0" />
                <el-option
                    v-for="item in optionsOfCustomerContacts" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="项目状态" prop="projectStatus">
              <el-select v-model="form.projectStatus" placeholder="请选择项目状态">
                <el-option
                    v-for="dict in scm_project_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="项目负责人" prop="ownerUserName">-->
<!--              <el-input v-model="form.ownerUserName" placeholder="请输入项目负责人名称" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="项目负责人" prop="ownerUserId">
              <el-select
                  v-model="form.ownerUserId" filterable remote reserve-keyword placeholder="请输入简称"
                  :remote-method="remoteMethodOfUser" @change="handleChangeOfUser" :loading="loading" >
                <el-option
                    v-for="item in optionsOfUser" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="采购类型" prop="purchaseCategory">
              <el-select v-model="form.purchaseCategory" placeholder="请选择采购类型">
                <el-option
                    v-for="dict in scm_purchase_category"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="部品类型" prop="demandType">
              <el-select v-model="form.demandType" placeholder="请选择部品类型">
                <el-option
                    v-for="dict in scm_demand_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预算金额" prop="budget">
              <el-input-number v-model="form.budget" placeholder="请输入预算金额" :min="0"
                               style="width: 100%;" :controls="false" :precision="2" class="number-right"
                               />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="我的币种" prop="myCurrency">
              <el-select v-model="form.myCurrency" placeholder="请选择我的币种">
                <el-option
                  v-for="dict in scm_currency"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户币种" prop="customerCurrency">
              <el-select v-model="form.customerCurrency" placeholder="请选择客户币种">
                <el-option
                  v-for="dict in scm_currency"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商币种" prop="supplierCurrency">
              <el-select v-model="form.supplierCurrency" placeholder="请选择供应商币种">
                <el-option
                    v-for="dict in scm_currency"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8"></el-col>

          <el-col :span="8">
            <el-form-item label="客户币种汇率" prop="customerCurrencyRate" label-width="120">
              <el-input-number v-model="form.customerCurrencyRate" placeholder="请输入客户币种汇率" type="number" :min="0" :controls="false" class="number-right" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商币种汇率" prop="supplierCurrencyRate" label-width="120">
              <el-input-number v-model="form.supplierCurrencyRate" placeholder="请输入供应商币种汇率" type="number" :min="0" :controls="false" class="number-right" />
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-form-item label="项目编号" prop="projectCode">-->
<!--          <el-input v-model="form.projectCode" placeholder="请输入项目编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="客户ID" prop="customerId">-->
<!--          <el-input v-model="form.customerId" placeholder="请输入客户ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="客户联系人ID" prop="customerContactId">-->
<!--          <el-input v-model="form.customerContactId" placeholder="请输入客户联系人ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="项目负责人ID" prop="ownerUserId">-->
<!--          <el-input v-model="form.ownerUserId" placeholder="请输入项目负责人ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="结束日期" prop="endDate">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.endDate"-->
<!--            type="date"-->
<!--            value-format="YYYY-MM-DD"-->
<!--            placeholder="请选择结束日期">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Projects">
import {addProjects, delProjects, getProjects, listProjects, updateProjects} from "@/api/scm/projects";
import {listCustomer} from "@/api/scm/customer";
import {listUser} from "@/api/system/user";
import {listCustomerContacts} from "@/api/scm/customerContacts";

const { proxy } = getCurrentInstance();
const { scm_project_status, scm_purchase_category, scm_demand_type, scm_currency } = proxy.useDict('scm_project_status', 'scm_purchase_category', 'scm_demand_type', 'scm_currency' );

const projectsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectCode: null,
    projectName: null,
    customerId: null,
    ownerUserId: null,
    startDate: null,
    endDate: null,
    projectStatus: null,
    purchaseCategory: null,
    demandType: null,
  },
  rules: {
    // projectCode: [
    //   { required: true, message: "项目编号不能为空", trigger: "blur" }
    // ],
    projectName: [
      { required: true, message: "项目名称不能为空", trigger: "blur" }
    ],
    customerTitle: [
      { required: true, message: "客户简写不能为空", trigger: "blur" }
    ],
    // ownerUserId: [
    //   { required: true, message: "项目负责人ID不能为空", trigger: "blur" }
    // ],
    ownerUserName: [
      { required: true, message: "项目负责人名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询项目管理列表 */
function getList() {
  loading.value = true;
  listProjects(queryParams.value).then(response => {
    projectsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    projectId: null,
    projectCode: null,
    projectName: null,
    customerId: null,
    customerTitle: null,
    customerContactId: null,
    customerContactName: null,
    ownerUserId: null,
    ownerUserName: null,
    myCurrency: "CNY",
    customerCurrency: "CNY",
    supplierCurrency: "CNY",
    customerCurrencyRate: 1,
    supplierCurrencyRate: 1,
    startDate: getCurrentDate(),
    endDate: null,
    projectStatus: 1,
    purchaseCategory: null,
    demandType: null,
    budget: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("projectsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.projectId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加项目管理";
  form.value.projectCode = generateProjectCode();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _projectId = row.projectId || ids.value
  getProjects(_projectId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改项目管理";
    console.log(form.value);
    initializeCustomerOptions(form.value.customerId);
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["projectsRef"].validate(valid => {
    if (valid) {
      if (form.value.projectId != null) {
        updateProjects(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProjects(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _projectIds = row.projectId || ids.value;
  proxy.$modal.confirm('是否确认删除项目管理编号为"' + _projectIds + '"的数据项？').then(function() {
    return delProjects(_projectIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/projects/export', {
    ...queryParams.value
  }, `projects_${new Date().getTime()}.xlsx`)
}

// 获取当前日期函数（格式 YYYY-MM-DD）
function getCurrentDate() {
  const today = new Date();
  const year = today.getFullYear();
  const month = String(today.getMonth() + 1).padStart(2, '0');
  const day = String(today.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

// 客户下拉框
const optionsOfCustomer = ref([]);
const customerList = ref([]);
const queryParamsOfCustomer = ref({});

function resetQueryOfCustomer() {
  queryParamsOfCustomer.value = {
    pageNum: 1,
    pageSize: 100,
    id: null,
    customerNo: null,
    name: null,
    nickname: null,
  }
}
function remoteMethodOfCustomer(query) {
  console.log('query', query);
  resetQueryOfCustomer();
  if (query !== ''){
    queryParamsOfCustomer.value.nickname = query.trim();
    listCustomerSelect();
  }
}
function initializeCustomerOptions(customerId) {
  console.log('customerId', customerId)
  resetQueryOfCustomer();
  queryParamsOfCustomer.value.id = customerId;
  listCustomerSelect();
  queryParamsOfCustomerContacts.value.customerId = customerId;
  listCustomerContactsSelect();
}
function handleChangeOfCustomer(value) {
  console.log('value', value);
  // form.value.customerId = value;
  form.value.customerTitle = customerList.value.find(customer => customer.id === value).nickname;
  form.value.customerContactId = 0;
  form.value.customerContactName = " ";
  // 根据 value 找到对应的 label
  // const selectedOption = optionsOfCustomer.value.find(option => option.value === value);
  // if (selectedOption) {
  //   form.value.customerTitle = selectedOption.label; // 将 label 赋值给 customerTitle
  // }
  queryParamsOfCustomerContacts.value.customerId = value;
  listCustomerContactsSelect();
}
function listCustomerSelect() {
  listCustomer(queryParamsOfCustomer.value).then(response => {
    customerList.value = response.rows;
    optionsOfCustomer.value = customerList.value.map(customer => ({
      label: customer.name,
      value: customer.id,
    }));
  });
}

//客户联系人下拉菜单
const optionsOfCustomerContacts = ref([]);
const customerContactsList = ref([]);
const queryParamsOfCustomerContacts = ref({});

function resetQueryOfCustomerContacts() {
  queryParamsOfCustomerContacts.value = {
    pageNum: 1,
    pageSize: 100,
    id: null,
    customerId: null,
  }
}
function remoteMethodOfCustomerContacts(query) {
  console.log('query', query);
  resetQueryOfCustomerContacts();
  if (query !== ''){
    queryParamsOfCustomerContacts.value.nickname = query.trim();
    // listCustomerContactsSelect();
  }
}
function initializeCustomerContactsOptions(customerContactsId) {
  console.log('customerContactsId', customerContactsId)
  resetQueryOfCustomerContacts();
  queryParamsOfCustomerContacts.value.id = customerContactsId;
  listCustomerContactsSelect();
}
function handleChangeOfCustomerContacts(value) {
  console.log('value', value);
  // form.value.customerContactsId = value;
  form.value.customerContactName = customerContactsList.value.find(customerContacts => customerContacts.id === value).contactName;
  // 根据 value 找到对应的 label
  // const selectedOption = optionsOfCustomerContacts.value.find(option => option.value === value);
  // if (selectedOption) {
  //   form.value.customerContactsTitle = selectedOption.label; // 将 label 赋值给 customerContactsTitle
  // }
}
function listCustomerContactsSelect() {
  listCustomerContacts(queryParamsOfCustomerContacts.value).then(response => {
    customerContactsList.value = response.rows;
    optionsOfCustomerContacts.value = customerContactsList.value.map(customerContacts => ({
      label: customerContacts.contactName,
      value: customerContacts.id,
    }));
  });
}

//负责人下拉菜单
const optionsOfUser = ref([]);
const userList = ref([]);
const queryParamsOfUser = ref({});

function resetQueryOfUser() {
  queryParamsOfUser.value = {
    pageNum: 1,
    pageSize: 100,
    userName: undefined,
    nickName: undefined,
    phonenumber: undefined,
    status: undefined,
    deptId: undefined
  }
}
function remoteMethodOfUser(query) {
  console.log('query', query);
  resetQueryOfUser();
  console.log('userList.value.length', userList.value.length);
  if (userList.value.length === 0){
    queryParamsOfUser.value.nickName = query.trim();
    listUserSelect();
  }
}
function initializeUserOptions(userId) {
  console.log('userId', userId)
  resetQueryOfUser();
  queryParamsOfUser.value.userId = userId;
  listUserSelect();
}
function handleChangeOfUser(value) {
  console.log('value', value);
  // form.value.userId = value;
  form.value.ownerUserName = userList.value.find(user => user.userId === value).nickName;
  // 根据 value 找到对应的 label
  // const selectedOption = optionsOfUser.value.find(option => option.value === value);
  // if (selectedOption) {
  //   form.value.userTitle = selectedOption.label; // 将 label 赋值给 userTitle
  // }
}
function listUserSelect() {
  listUser(queryParamsOfUser.value).then(response => {
    userList.value = response.rows;
    optionsOfUser.value = userList.value.map(user => ({
      label: user.nickName,
      value: user.userId,
    }));
  });
}

// 生成项目编号
function generateProjectCode() {
  const today = new Date();

  // 获取年份并取后四位
  const year = today.getFullYear().toString().padStart(4, '0');

  // 获取月份并补零
  const month = (today.getMonth() + 1).toString().padStart(2, '0');

  // 获取日期并补零
  const day = today.getDate().toString().padStart(2, '0');

  // 拼接项目编号
  return `PJ-${year}${month}${day}-nn`;
}


// 列显隐信息
const columns = ref([
  { key: 0, label: `项目名称`, visible: true },
  { key: 1, label: `开始日期`, visible: true },
  { key: 2, label: `结束日期`, visible: true },
  { key: 3, label: `项目状态`, visible: true },
  { key: 4, label: `采购类型`, visible: true },
  { key: 5, label: `部品类型`, visible: true },
  { key: 6, label: `备注`, visible: true }
]);

//初始化
getList();
</script>
